﻿package com.hh.bss.auth.client;

import java.util.Date;
import java.util.List;

import com.hh.bss.config.Environment;
import com.hh.bss.util.DESHelper;
import com.hh.bss.util.DateHelper;
import com.hh.bss.util.StringHelper;





public class OnlineUser {
	private Integer id = new Integer(0);
	public void setId(Integer id){
		this.id = id;
	}
	public Integer getId(){
		return this.id;
	}
	//
	private String username = null;
	public void setUsername(String username){
		this.username = username;
	}
	public String getUsername(){
		return this.username;
	}
	//
	private String password = null;
	public void setPassword(String password){
		this.password = password;
	}
	public String getPassword(){
		return this.password;
	}
	//
	private String name = null;
	public void setName(String name){
		this.name = name;
	}
	public String getName(){
		return this.name;
	}
	public static Integer SUPER = new Integer(1);
	public static Integer NORMAL = new Integer(0);
	private Integer type = NORMAL;
	public void setType(Integer type){
		this.type = type;
	}
	public Integer getType(){
		return this.type;
	}
	public boolean isSuperUser(){
		return SUPER.equals(getType());
	}
	//
	private Date signInTime = null;
	public void setSignInTime(Date signInTime){
		this.signInTime = signInTime;
	}
	public Date getSignInTime(){
		return this.signInTime;
	}
	public String getSignInTime(String format){
		return DateHelper.toString(getSignInTime(), format);
	}
	private Integer historyId = new Integer(0);
	public void setHistoryId(Integer historyId){
		this.historyId = historyId;
	}
	public Integer getHistoryId(){
		return this.historyId;
	}
	private String roleIds;
	
	public String getRoleIds() {
		return roleIds;
	}
	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}
	public static String encode(OnlineUser onlineUser){
		StringBuffer text = new StringBuffer();
		text.append(onlineUser.getId());//用户ID
		text.append(";").append(onlineUser.getUsername());//用户名
		text.append(";").append(onlineUser.getName());//真实姓名
		text.append(";").append(onlineUser.getType());//用户类型
		text.append(";").append(onlineUser.getSignInTime("yyyy-MM-dd HH:mm:ss"));//登录时间
		text.append(";").append(onlineUser.getHistoryId());//登录ID
		text.append(";").append(onlineUser.getRoleIds());//roleid
		//得到加密后的密文
		String encryptStr=null;
		try {
			encryptStr=DESHelper.byte2hex(DESHelper.desEncrypt(text.toString().getBytes(), Environment.SECURE_KEY));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return encryptStr;
	}
	public static OnlineUser decode(String in){
		if(in == null) return null;
		//按时间来解密
		String text=null;
		try {
			text = new String(DESHelper.desDecode(DESHelper.hex2byte(in), Environment.SECURE_KEY));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(text == null) return null;
		String[] properties = text.split(";");
		if(properties == null || properties.length < 6 || properties[0] == null) return null;
		OnlineUser onlineUser = new OnlineUser();
		onlineUser.setId(StringHelper.parseInteger(properties[0], 0));
		onlineUser.setUsername(properties[1]);
		onlineUser.setName(properties[2]);
		onlineUser.setType(StringHelper.parseInteger(properties[3],0));
		onlineUser.setSignInTime(DateHelper.toDate(properties[4], "yyyy-MM-dd HH:mm:ss"));
		onlineUser.setHistoryId(StringHelper.parseInteger(properties[5], 0));
		if (properties.length > 6) onlineUser.setRoleIds(properties[6]); 
		return onlineUser;
	}
	
}
